<template >
    <div>
        <h1>人员列表</h1>
        <h3 style="color:red">Count组件的求和{{all}}</h3>
        <h3>列表中第一个人的名字{{firstPersonName}}</h3>
        <input type="text" placeholder="请输入名字" v-model="name">
        <button @click="addPerson">添加</button>
        <button @click="addPersonWang">添加一个姓王的人</button>
        <button @click="addRandumGuy"> 添加一个随机的人</button>
        <ul>
            <li v-for="p in personList" :key="p.id">{{p.name}}</li>
        </ul>
    </div>
</template>
<script>
import {mapState} from 'vuex'
import {nanoid} from 'nanoid';
export default {
    name:'Person',
    data() {
        return {
            name:''
        }
    },
    computed:{
        personList(){
            return this.$store.state.personAbout.personList
        },
        all(){
            return this.$store.state.countAbout.sum
        },
        firstPersonName(){
            return this.$store.getters['personAbout/firstPersonName']
        }

    },
    methods:{
        addPerson(){
            if (this.name.trim()) {
                const p = {id:nanoid(),name:this.name}
                this.$store.commit('personAbout/ADD_PERSON',p)
                this.name = ''
            }
        },
        addPersonWang(){
            if (this.name.trim()) {
                const p = {id:nanoid(),name:this.name}
                this.$store.dispatch('personAbout/addpersonWang',p)
                this.name = ''
            }
        },
        addRandumGuy(){
            this.$store.dispatch('personAbout/addPersonServer')
        }
    }
}
</script>
